##Intragroup_Conflict_Leadership_Study_1b.R
##Study 1b, Pardon - Hyp Gov + Trump (GOP Sample)

##Load data
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files")
data1b.finish<- read.csv("Study_1b.csv", header=TRUE)

###############################
##Appendix Table D2
##Summary stats and balance tables (partisans only) 
##Balance using chi-squared, exclude independents since excluded in analysis
library(MASS) ##(only run for this, causes issues with tdyr/dplyr, need to detach after)
##gender
balance.gender<- table(data1b.finish$female, data1b.finish$full.treatment)
prop.table(balance.gender, 2)
chisq.test(balance.gender)
##education
balance.educ<- table(data1b.finish$educ, data1b.finish$full.treatment)
prop.table(balance.educ, 2)
chisq.test(balance.educ)
##income
balance.income<- table(data1b.finish$income, data1b.finish$full.treatment)
prop.table(balance.income, 2)
chisq.test(balance.income)
##age (categorical)
balance.age.cat<- table(data1b.finish$age.cat, data1b.finish$full.treatment)
prop.table(balance.age.cat, 2)
chisq.test(balance.age.cat)

##sample demographics
##gender
sample.gender<- table(data1b.finish$female)
prop.table(sample.gender)
##education
sample.educ<- table(data1b.finish$educ)
prop.table(sample.educ)
##income
sample.income<- table(data1b.finish$income)
prop.table(sample.income)
##age (categorical)
sample.age.cat<- table(data1b.finish$age.cat)
prop.table(sample.age.cat)

detach("package:MASS", unload=TRUE)
detach("package:psych", unload=TRUE)

#########################################
##Appendix Table D3
##Summary Stats (post-dissent measures)

summary(data1b.finish$ft_leader2)
length(na.omit(data1b.finish$ft_leader2))
sd(data1b.finish$ft_leader2, na.rm=TRUE)

summary(data1b.finish$vote_L_2)
length(na.omit(data1b.finish$vote_L_2))
sd(data1b.finish$vote_L_2, na.rm=TRUE)

summary(data1b.finish$appropriate)
length(na.omit(data1b.finish$appropriate))
sd(data1b.finish$appropriate, na.rm=TRUE)

summary(data1b.finish$ft_critic)
length(na.omit(data1b.finish$ft_critic))
sd(data1b.finish$ft_critic, na.rm=TRUE)

summary(data1b.finish$vote_critic)
length(na.omit(data1b.finish$vote_critic))
sd(data1b.finish$vote_critic, na.rm=TRUE)


###############################
##Appendix Table D1
##N by conditions
library(tidyverse)
study3.N<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, OwnParty) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(Condition.short!="Aust") %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(n=n())
study3.N
sum(study3.N$n)

#####################################
##Load packages for analyses
##Analysis with dplyr
library(dplyr)
library(tidyr)
library(infer)
library(ggplot2)
library(gridExtra)

##for regression tables
source("regtable.R")

##set directory for output
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files/Output")

##leader affect, post response
study1b.resp.all.ft_leader2<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, ft_leader2) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))
study1b.resp.all.ft_leader2 

##plot
trump_ld_ft<-ggplot(study1b.resp.all.ft_leader2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  facet_grid(~ Condition.short) + # split into plots, side by side
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Leader Feeling Thermometer") +
  ggtitle("(a) Leader Feeling Thermometer")

##t-test
t.test(data1b.finish$ft_leader2[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
t.test(data1b.finish$ft_leader2[data1b.finish$Condition=="Trump"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Trump"])


##Is effect of dissent different in Trump vs. Gov?
study1b.ft_leader2.conditions<- lm(ft_leader2 ~ ResponseCondition*Condition, 
                                  data=data1b.finish)
summary(study1b.ft_leader2.conditions) ##rebuke has a significantly smaller effect for Trump (smaller rally)


###########################################
##leader vote, post response
study1b.resp.all.vote_L_2<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, vote_L_2) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))
study1b.resp.all.vote_L_2 

##plot
trump_ld_vote<-ggplot(study1b.resp.all.vote_L_2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  facet_grid(~ Condition.short) + # split into plots, side by side
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Leader Vote") +
  ggtitle("(b) Leader Vote")

##t-test
t.test(data1b.finish$vote_L_2[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
t.test(data1b.finish$vote_L_2[data1b.finish$Condition=="Trump"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Trump"])

##Is effect of dissent different in Trump vs. Gov?
study1b.vote_L_2.conditions<- lm(vote_L_2 ~ ResponseCondition*Condition, 
                                data=data1b.finish)
summary(study1b.vote_L_2.conditions)


###########################################
##critic affect, post response
study1b.resp.all.ft_critic<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, ft_critic) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))
study1b.resp.all.ft_critic

##plot
trump_al_ft<-ggplot(study1b.resp.all.ft_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  facet_grid(~ Condition.short) + # split into plots, side by side
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Feeling Thermometer") +
  ggtitle("(c) Assembly Leader Feeling Thermometer")

##t-test
t.test(data1b.finish$ft_critic[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
t.test(data1b.finish$ft_critic[data1b.finish$Condition=="Trump"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Trump"])


###########################################
##critic vote, post response
study1b.resp.all.vote_critic<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, vote_critic) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))
study1b.resp.all.vote_critic

trump_al_vote<-ggplot(study1b.resp.all.vote_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  facet_grid(~ Condition.short) + # split into plots, side by side
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Vote") +
  ggtitle("(d) Assembly Leader Vote")

##t-test
t.test(data1b.finish$vote_critic[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
t.test(data1b.finish$vote_critic[data1b.finish$Condition=="Trump"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Trump"])



#############################################
##Output figures for each study
blank<- ggplot() + theme_void()

##Pardon Trump GOP
grid.arrange(trump_ld_ft, trump_ld_vote, trump_al_ft, trump_al_vote, nrow=2)
fig.study1b.g<- arrangeGrob(trump_ld_ft, trump_ld_vote, trump_al_ft, trump_al_vote, nrow=2)
ggsave("Fig4_hyp_trump_all_DV.png", fig.study1b.g, height=10, width=12)


###########################################
##Mechanisms

##appropriateness of response, post response - mechanisms
study1b.resp.all.appropriate<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Governor", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, appropriate) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))
study1b.resp.all.appropriate

##plot
trump_approp<-ggplot(study1b.resp.all.appropriate) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  facet_grid(~ Condition.short) + # split into plots, side by side
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Appropriateness of Response") +
  ggtitle("(a) Appropropriateness of Assembly Leader Response")

##t-test
t.test(data1b.finish$appropriate[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
t.test(data1b.finish$appropriate[data1b.finish$Condition=="Trump"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Trump"])

###########################################
##who best serves the party histogram
##plot distribution (N) by condition:
study1b.resp.all.best.serve.party<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Gov", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, best.serve.party.cat) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) 

study1b.resp.all.best.serve.party
##set order
study1b.resp.all.best.serve.party$value<- factor(study1b.resp.all.best.serve.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot
trump_best_serves<- ggplot(study1b.resp.all.best.serve.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = c(ResponseCondition.new)))  + 
  facet_grid(~ Condition.short + ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(b) Who Best Serves the Party")
trump_best_serves

##calc proportion
study1b.resp.all.best.serve.party.prop<- study1b.resp.all.best.serve.party %>%
  count(Condition.short, ResponseCondition.new, value, name="n_response_best_serves")
study1b.resp.all.best.serve.party.prop
#########Governor condition
#####loyalty condition
##leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[1]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##assembly leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[2]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##both
study1b.resp.all.best.serve.party.prop$n_response_best_serves[3]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##neither
study1b.resp.all.best.serve.party.prop$n_response_best_serves[4]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
######dissent condition
##leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[5]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##assembly leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[6]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##both
study1b.resp.all.best.serve.party.prop$n_response_best_serves[7]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##neither
study1b.resp.all.best.serve.party.prop$n_response_best_serves[8]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[5:8])

#########Trump condition
#####loyalty condition
##leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[9]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[9:12])
##assembly leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[10]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[9:12])
##both
study1b.resp.all.best.serve.party.prop$n_response_best_serves[11]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[9:12])
##neither
study1b.resp.all.best.serve.party.prop$n_response_best_serves[12]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[9:12])
######dissent condition
##leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[13]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[13:16])
##assembly leader
study1b.resp.all.best.serve.party.prop$n_response_best_serves[14]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[13:16])
##both
study1b.resp.all.best.serve.party.prop$n_response_best_serves[15]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[13:16])
##neither
study1b.resp.all.best.serve.party.prop$n_response_best_serves[16]/sum(study1b.resp.all.best.serve.party.prop$n_response_best_serves[13:16])


###########################################
##who embarrasses the party histogram
##plot distribution (N) by condition:
study1b.resp.all.embarrass.party<- data1b.finish %>%
  mutate(Condition.short=factor(Condition, levels=c("Governor", "Trump"), labels=c("Gov", "Trump"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(Condition.short, ResponseCondition.new, embarrass.party.cat) %>%
  gather("measure", "value", -Condition.short, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(Condition.short, ResponseCondition.new) 

study1b.resp.all.embarrass.party
##set order
study1b.resp.all.embarrass.party$value<- factor(study1b.resp.all.embarrass.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot
trump_embarrass<- ggplot(study1b.resp.all.embarrass.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = c(ResponseCondition.new)))  + 
  facet_grid(~ Condition.short + ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(c) Who Embarrasses the Party")
trump_embarrass

##calc proportion
study1b.resp.all.embarrass.party.prop<- study1b.resp.all.embarrass.party %>%
  count(Condition.short, ResponseCondition.new, value, name="n_response_embarrass")
study1b.resp.all.embarrass.party.prop
#########Governor condition
#####loyalty condition
##leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[1]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##assembly leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[2]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##both
study1b.resp.all.embarrass.party.prop$n_response_embarrass[3]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##neither
study1b.resp.all.embarrass.party.prop$n_response_embarrass[4]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
######dissent condition
##leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[5]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##assembly leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[6]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##both
study1b.resp.all.embarrass.party.prop$n_response_embarrass[7]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##neither
study1b.resp.all.embarrass.party.prop$n_response_embarrass[8]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[5:8])

#########Trump condition
#####loyalty condition
##leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[9]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[9:12])
##assembly leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[10]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[9:12])
##both
study1b.resp.all.embarrass.party.prop$n_response_embarrass[11]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[9:12])
##neither
study1b.resp.all.embarrass.party.prop$n_response_embarrass[12]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[9:12])
######dissent condition
##leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[13]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[13:16])
##assembly leader
study1b.resp.all.embarrass.party.prop$n_response_embarrass[14]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[13:16])
##both
study1b.resp.all.embarrass.party.prop$n_response_embarrass[15]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[13:16])
##neither
study1b.resp.all.embarrass.party.prop$n_response_embarrass[16]/sum(study1b.resp.all.embarrass.party.prop$n_response_embarrass[13:16])


###########################################
##Plot figures of mechanisms
##Pardon Trump GOP
grid.arrange(trump_approp, trump_best_serves, trump_embarrass, nrow=1)
trump_mech<- arrangeGrob(trump_approp, trump_best_serves, trump_embarrass, nrow=1)
ggsave("FigD1_pardon_trump_gop_mechanisms.png", trump_mech, height=5, width=15)



######################################################
##              Within subjects analysis            ##
######################################################
##Appendix H, Table H1

##Holding effect of Response constant across stage 1 treatment groups
#Feeling thermometer
study1b.ft<- lm(ft_leader2 ~ ResponseCondition + ft_leader1 + Condition,
               data=data1b.finish)
summary(study1b.ft)

#Vote
study1b.vote<- lm(vote_L_2 ~ ResponseCondition + vote_L_1 +  Condition,
                 data=data1b.finish)
summary(study1b.vote)





#############################################
##              Power calc                 ##
#############################################
##Appendix I, Table I1

##critic FT
##Governor - Loyal
summary(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"])
sd(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"], na.rm=TRUE)
length(na.omit(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"]))
##Governor - Dissent
summary(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"])
sd(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"], na.rm=TRUE)
length(na.omit(data1b.finish$ft_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"]))
##Power analysis
library(stats)
power.t.test(n=108, sig.level=0.05, power=0.80, sd=0.29)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
##could have detected a mean difference of 0.109 (observed difference was 0.012)
t.test(data1b.finish$ft_critic[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
0.3337963-0.3214173

##critic vote
##Governor - Loyal
summary(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"])
sd(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"], na.rm=TRUE)
length(na.omit(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Agree"]))
##Governor - Dissent
summary(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"])
sd(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"], na.rm=TRUE)
length(na.omit(data1b.finish$vote_critic[data1b.finish$Condition=="Governor" & data1b.finish$ResponseCondition=="Rebuke"]))
##Power analysis
library(stats)
power.t.test(n=110, sig.level=0.05, power=0.80, sd=0.31)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
##could have detected a mean difference of 0.118 (observed difference was 0.059)
t.test(data1b.finish$vote_critic[data1b.finish$Condition=="Governor"] ~ data1b.finish$ResponseCondition[data1b.finish$Condition=="Governor"])
0.4393618 - 0.4986721 

#####################################
##Comparison of pre-treatment Trump FT to ANES
summary(data1b.finish$pre_trump_ft)
sd(data1b.finish$pre_trump_ft)

